<template>
	<view class="container">
		<view class="product" v-for="product in details.items" :key="product.productId">
			<image :src="product.productImage" mode="aspectFill" class="img"></image>
			<view class="text">
				<view class="name ell">{{product.productName}}</view>
				<view class="specifications ell" v-if="product.normal">{{product.normal}}</view>	
				<view class="num">x{{product.num}}</view>
			</view>
			<view class="price">¥{{product.price}}</view>
		</view>
		<view class="infor-wrapper">
			<view class="infor-item border-bottom" v-if="details.status != 1">
				<view class="title">申请类型</view>
				<picker class="picker-wrapper" :value="typeIndex" :range="typeList" :range-key="'title'" @change="bindTypePickerChange">
					<view class="picker">
						<view class="text" :class="{'placehol': !typeIndex}">{{typeList[typeIndex].title || '请选择'}}</view>
						<image src="/static/next06.png" class="icon"></image>	
					</view>
				</picker>
			</view>
			<view class="infor-item border-bottom">
				<view class="title">退款原因</view>
				<picker class="picker-wrapper" :value="reasonIndex" :range="reasonList" :range-key="'title'" @change="bindReasonPickerChange">
					<view class="picker">
						<view class="text" :class="{'placehol': !reasonIndex}">{{reasonList[reasonIndex].title || '请选择'}}</view>
						<image src="/static/next06.png" class="icon"></image>	
					</view>
				</picker>
			</view>
			<view class="amount-wrapper">
				退款金额: <text class="oran">{{details.totalFee}}元</text>
			</view>
			<view class="amount-input border-bottom">
				<input type="number" v-model="amount" :placeholder="`不可修改，最多${details.totalFee}元`" disabled="true" />
			</view>
			<view class="infor-item">
				<view class="title">退款说明: </view>
				<input type="text" v-model="refundMemo" placeholder="选填" />
			</view>
		</view>
		<view class="img-wrapper">
			<view class="title">上传凭证</view>
			<view class="img-list">
				<view class="img-item" v-for="(image, index) in images" :key="index">
					<view class="delet" @click="handleCancel(index)">-</view>
					<image :src="image" mode="aspectFill" class="img"></image>
				</view>
				<view class="img-item btn" @click="handleUploadSingle">
					<image src="/static/upload02.png" class="img"></image>
					<text class="desc">上传凭证\n（最多6张）</text>
				</view>
			</view>
		</view>
		<button type="default" :disabled="flag" class="bottom-btn" @click="submit">提交</button>
	</view>
</template>

<script>
	import { frontDetail, doRefund } from '@/api/order.js'
	import upload from '@/mixins/upload.js'
	export default {
		mixins: [upload],
		data() {
			return {
				orderId: '',
				flag: false,
				refundMemo: '',
				details: {},
				amount: '',
				typeIndex: '',
				typeList: [
					{
						title: '仅退款',
						type: 1
					},
					{
						title: '退款退货',
						type: 2
					}
				],
				reasonIndex: '',
				reasonList: [
					{
						title: '不想要了/拍错了',
						type: 1
					},
					{
						title: '退运费/退差价',
						type: 2
					},
					{
						title: '收到商品破损',
						type: 3
					},
					{
						title: '收到商品与描述不符',
						type: 4
					},
					{
						title: '商品质量问题',
						type: 5
					},
					{
						title: '未按约定时间发货',
						type: 6
					},
					{
						title: '发票问题',
						type: 7
					},
					{
						title: '其他',
						type: 8
					}
				]
			}
		},
		onLoad(option) {
			this.orderId = option.orderId
			this.getDetails()
		},
		onShow() {
		},
		methods: {
			submit() {
				if (!this.typeIndex && this.details.status != 1) {
					uni.showToast({
						title: '请选择申请类型',
						icon: 'none',
						duration: 2000
					})
					return
				}
				if (!this.reasonIndex) {
					uni.showToast({
						title: '请选择退款原因',
						icon: 'none',
						duration: 2000
					})
					return
				}
				const imagesStr = this.images.join(',')
				const data = {
				  orderId: this.details.id,
				  type: this.details.status == 1 ? 1 : this.typeList[this.typeIndex].type,
				  reason: this.reasonList[this.reasonIndex].title,
				  images: imagesStr,
				  refundMemo: this.refundMemo
				}
				console.log('data',data)
				this.flag = true
				doRefund(data).then(respanse => {
					uni.showToast({
						title: '申请提交成功！',
						icon: 'success',
						duration: 2000
					})
					let pages = getCurrentPages();  //获取所有页面栈实例列表
					let nowPage = pages[ pages.length - 1];  //当前页页面实例
					let prevPage = pages[ pages.length - 2 ];  //上一页页面实例
					prevPage.$vm.isOrderHandle = true // 监听是否选择地址
					setTimeout(() => {
						uni.navigateBack()
					}, 2000)
				}).catch(err => {
					this.flag = false
				})
			},
			
			getDetails() {
				const data = {
				  orderId: this.orderId
				}
				frontDetail(data).then(respanse => {
					this.details = respanse.result
				})
			},
			
			bindTypePickerChange: function(e) {
				this.typeIndex = e.target.value
			},
			bindReasonPickerChange: function(e) {
				this.reasonIndex = e.target.value
			}
		}
	}
</script>

<style lang="scss" scoped>
	.border-bottom{
		&::after{
			left: 20rpx;
			right: 20rpx;
		}
	}
	.product{
		display: flex;
		padding: 36rpx 23rpx 33rpx 32rpx;
		margin: 30rpx 25rpx 0;
		background-color: #fff;
		border-radius: 10rpx;
		overflow: hidden;
		.img{
			width: 156rpx;
			height: 156rpx;
			border-radius: 10rpx;
			background-color: #F5F5F5;
		}
		.text{
			flex: 1;
			padding-top: 7rpx;
			margin-left: 39rpx;
			overflow: hidden;
			.name{
				line-height: 38rpx;
				font-size: 28rpx;
				font-weight: bold;
				color: #080808;
			}
			.specifications{
				margin-top: 6rpx;
				line-height: 32rpx;
				font-size: 22rpx;
				color: #818181;
			}
			.num{
				margin-top: 19rpx;
				line-height: 38rpx;
				font-size: 28rpx;
				color: #000000;
			}
		}
		.price{
			line-height: 38rpx;
			font-size: 30rpx;
			font-weight: bold;
			color: #080808;
		}
	}
	.infor-wrapper{
		margin: 20rpx 25rpx 0;
		background-color: #fff;
		border-radius: 10rpx;
		overflow: hidden;
		.infor-item{
			position: relative;
			display: flex;
			.title{
				padding-left: 28rpx;
				line-height: 84rpx;
				font-size: 28rpx;
				color: #333333;
			}
			input{
				flex: 1;
				padding-left: 10rpx;
				height: 84rpx;
				font-size: 28rpx;
				color: #333333;
			}
			.picker-wrapper{
				flex: 1;
				.picker{
					display: flex;
					align-items: center;
					flex: 1;
					height: 84rpx;
					line-height: 84rpx;
					text-align: right;
					font-size: 28rpx;
					color: #333333;
					.text{
						flex: 1;
						margin-right: 28rpx;
						&.placehol{
							color: #999999;
						}
					}
					.icon{
						margin-right: 28rpx;
						width: 16rpx;
						height: 29rpx;
					}
				}
			}
		}
	}
	.amount-wrapper{
		padding: 22rpx 20rpx 22rpx 25rpx;
		line-height: 38rpx;
		font-size: 28rpx;
		color: #333333;
		.oran{
			padding-left: 10rpx;
			color: #EA5D2E;
		}
	}
	.amount-input{
		position: relative;
		input{
			display: block;
			padding: 0 20rpx 0 25rpx;
			width: 100%;
			height: 84rpx;
			box-sizing: border-box;
			font-size: 26rpx;
			color: #333;
		}
		
	}
	.img-wrapper{
		padding: 19rpx 0 8rpx 24rpx;
		margin: 20rpx 25rpx 0;
		background-color: #fff;
		border-radius: 10rpx;
		.title{
			margin-bottom: 23rpx;
			height: 38rpx;
			line-height: 38rpx;
			font-size: 28rpx;
			color: #333333;
		}
		.img-list{
			display: flex;
			flex-wrap: wrap;
			.img-item{
				position: relative;
				margin: 0 35rpx 30rpx 0;
				width: 160rpx;
				height: 160rpx;
				.img{
					width: 100%;
					height: 100%;
				}
				.delet{
					position: absolute;
					right: -20rpx;
					top: -20rpx;
					width: 50rpx;
					height: 50rpx;
					line-height: 50rpx;
					text-align: center;
					font-size: 30rpx;
					color: #fff;
					background: linear-gradient(90deg, #EB7C3F, #EA5D2E);
					border-radius: 50%;
				}
				.desc{
					position: absolute;
					left: 0;
					right: 0;
					top: 81rpx;
					line-height: 22rpx;
					text-align: center;
					font-size: 20rpx;
					color: #999999;
				}
			}
		}
	}
	.bottom-btn{
		display: block;
		margin: 52rpx auto 0;
		width: 690rpx;
		height: 90rpx;
		line-height: 90rpx;
		font-size: 32rpx;
		color: #fff;
		background: linear-gradient(90deg, #EB7C3F, #EA5D2E);
		border-radius: 45px;
	}
</style>
